//
//  LDSLaunchH5ViewController.m
//  TestCategory
//
//  Created by kai_leedarson on 2017/10/19.
//  Copyright © 2017年 maple_leedarson. All rights reserved.
//

#import "LDSLaunchH5ViewController.h"
#import "LDSLoginViewController.h"
#import "LDSRegisterViewController.h"

#import <WebKit/WebKit.h>

@interface LDSLaunchH5ViewController ()
@property (nonatomic, strong) WKWebView *webView;
@end

@implementation LDSLaunchH5ViewController

#pragma mark - lazy init
-(WKWebView *)webView {
    if (_webView == nil) {
        WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
        config.preferences = [[WKPreferences alloc] init];
        config.processPool = [[WKProcessPool alloc] init];
        config.userContentController = [[WKUserContentController alloc] init];
        [config.userContentController addScriptMessageHandler:(id <WKScriptMessageHandler>)self name:@"AppModel"];
        _webView = [[WKWebView alloc] initWithFrame:self.view.bounds
                                          configuration:config];
        _webView.UIDelegate = (id <WKUIDelegate>)self;
        _webView.scrollView.scrollEnabled =NO;
    }
    return _webView;
}

#pragma mark - life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    if (!_pathStr) {
        _pathStr =@"H5HTML6/html/start/html/index";
        [self performSelector:@selector(loadLoginSignUpView) withObject:nil afterDelay:1.0];
    } else {
        CGRect frame = self.webView.frame;
        frame.size.height = self.view.bounds.size.height-64;
        self.webView.frame = frame;
    }
    NSURL *path = [[NSBundle mainBundle] URLForResource:_pathStr withExtension:@"html"];
    [self.webView loadRequest:[NSURLRequest requestWithURL:path]];
    [self.view addSubview:self.webView];
}

- (void)loadLoginSignUpView {
    NSURL *path = [[NSBundle mainBundle] URLForResource:@"H5HTML6/html/loginSign/html/loginSign" withExtension:@"html"];
    [self.webView loadRequest:[NSURLRequest requestWithURL:path]];
}

#pragma mark - WKScriptMessageHandler
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
    if ([message.name isEqualToString:@"AppModel"]) {
        NSLog(@"%@", message.body);
        NSDictionary * messageBody =message.body;
        NSString * messageStr =messageBody[@"body"];
        if ([messageStr isEqualToString:@"log_skip"]) {
            [self skipLogin];
        } else if ([messageStr isEqual:@"log"]) {
            [self pushToVCLogin];
        } else if ([messageStr isEqual:@"sign"]){
            [self pushToVCRegister];
        } else if (messageStr.length >= 4 && [[messageStr substringToIndex:4] isEqualToString:@"next"]){
        }
    }
}

- (void)skipLogin {

}

- (void)pushToVCLogin {
    UIViewController *vc = [LDSLoginViewController new] ;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)pushToVCRegister {
    UIViewController *vc = [LDSRegisterViewController new] ;
    [self.navigationController pushViewController:vc animated:YES];
}

@end
